<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="规格型号" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入规格型号"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内码" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入内码"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单规格" prop="flex">
        <el-input
          v-model="queryParams.flex"
          placeholder="请输入订单规格"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="长度" prop="len">
        <el-input
          v-model="queryParams.len"
          placeholder="请输入长度"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="success"
          :loading="exportLoading"
          plain
          @click="handleExport"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-table v-loading="detailLoading" :data="list">
      <el-table-column type="index" label="序号" align="center" width="55" />
      <el-table-column label="订单编号" align="center" prop="orderCode" />
      <el-table-column label="规格型号" align="center" prop="type" />
      <el-table-column label="订单规格" align="center" prop="flex" />
      <el-table-column label="长度" align="center" prop="len" />
      <el-table-column label="内码" align="center" prop="internalCode" />
      <el-table-column label="工位" align="center" prop="workstationName" />
      <el-table-column label="数值" align="center" prop="workstationValue" />
      <el-table-column label="操作时间" align="center" prop="operatorTime" />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" :data="defectiveProductRecordList" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column type="index" label="序号" align="center" width="55" />
      <el-table-column label="订单号" align="center" prop="orderCode" />
      <el-table-column label="规格型号" align="center" prop="flex" />
      <el-table-column label="订单详情ID" align="center" prop="orderDetailId" />
      <el-table-column label="内码" align="center" prop="internalCode" />
      <el-table-column label="不良品原因" align="center" prop="defectiveReason" />
      <el-table-column label="操作员" align="center" prop="operatorUser" />
      <el-table-column
        label="操作时间"
        align="center"
        prop="operatorTime"
        :formatter="dateFormatter"
        width="180px"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="defectiveProductRecordTotal"
      v-model:page="defectiveProductRecordQueryParams.pageNo"
      v-model:limit="defectiveProductRecordQueryParams.pageSize"
      @pagination="getDefectiveProductRecordList"
    />
  </ContentWrap>

</template>

<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import * as internalCodeApi from '@/api/hcxly/internalCode/Index'
import { DefectiveProductRecordApi } from '@/api/hcxly/defectiveproductrecord'
import download from "@/utils/download";

defineOptions({ name: 'internalDetail' })

const dialogVisible = ref(false) // 弹窗的是否展示

const { query } = useRoute() // 查询参数
const detailLoading = ref(false) // 表单的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const message = useMessage() // 消息弹窗

const defectiveProductRecordList = ref([])

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const orderId = query.id as unknown as string // 从 URL 传递过来的 id 编号
const finishStatus = query.finishStatus as unknown as string
const cacheOrderCode = query.cacheOrderCode as unknown as string
const labelType = query.labelType as unknown as string
const labelTypeValue = query.labelTypeValue as unknown as number
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type: '',
  id: '',
  orderDetailId: orderId,
  flex: '',
  len: null,
  finishStatus: finishStatus,
  cacheOrderCode: cacheOrderCode,
  labelType: labelType,
  labelTypeValue: labelTypeValue
})


const defectiveProductRecordTotal = ref(0) // 列表的总页数
const defectiveProductRecordQueryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderCode: '',
  flex: '',
  orderDetailId: orderId,
  internalCode: '',
  defectiveReason: '',
  operatorUser: '',
  operatorTime: ''
})


/** 查询列表 */
const getList = async () => {
  detailLoading.value = true
  try {
    const data = await internalCodeApi.listWorkstationDetail(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    detailLoading.value = false
  }
}

const getDefectiveProductRecordList = async () => {
  const data = await DefectiveProductRecordApi.getDefectiveProductRecordPage(defectiveProductRecordQueryParams)
  defectiveProductRecordList.value = data.list
  defectiveProductRecordTotal.value = data.total

}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await internalCodeApi.exportWorkstationDetail(queryParams);
    console.log('data',data)
    download.excel(data, '产量总排程单三级报表.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}


/** 初始化 **/
onMounted(() => {
  getList();
  getDefectiveProductRecordList();
})
</script>
